package cn.stylefeng.guns.modular.order.controller;

import cn.hutool.core.util.IdUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.order.model.params.TbFreightParam;
import cn.stylefeng.guns.modular.order.model.result.TbFreightResult;
import cn.stylefeng.guns.modular.order.model.result.WeightPriceResult;
import cn.stylefeng.guns.modular.order.service.TbFreightService;
import cn.stylefeng.guns.modular.order.service.WeightPriceService;
import cn.stylefeng.guns.sys.modular.tao.entity.Region;
import cn.stylefeng.guns.sys.modular.tao.service.RegionService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 运费表控制器
 *
 * @author zhou
 * @Date 2019-08-05 15:02:24
 */
@Controller
@RequestMapping("/tbFreight")
public class TbFreightController extends BaseController {

    private String PREFIX = "/order/tbFreight";

    @Autowired
    private TbFreightService tbFreightService;

    @Autowired
    private WeightPriceService weightPriceService;
    @Autowired
    private RegionService RegionService;
    /**
     * 获取重量与价格
     * @return
     */
    private List<WeightPriceResult> getWeightPriceResult() {
        return weightPriceService.findListBySpec(null);
    }

    /**
     * 跳转到主页面
     *
     * @author zhou
     * @Date 2019-08-05
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/tbFreight.html";
    }

    /**
     * 新增页面
     *
     * @author zhou
     * @Date 2019-08-05
     */
    @RequestMapping("/add")
    public String add(Model model) {
       /* model.addAttribute("weightPriceResults", getWeightPriceResult());*/
        return PREFIX + "/tbFreight_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhou
     * @Date 2019-08-05
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
        model.addAttribute("weightPriceResults", getWeightPriceResult());
        return PREFIX + "/tbFreight_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhou
     * @Date 2019-08-05
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TbFreightParam tbFreightParam) {

        Region Region = RegionService.getAreaNameByCode(tbFreightParam.getDestination());
        tbFreightParam.setArea("全国");
        tbFreightParam.setDestination(Region.getRegionName());
        this.tbFreightService.add(tbFreightParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhou
     * @Date 2019-08-05
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TbFreightParam tbFreightParam) {
        this.tbFreightService.update(tbFreightParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhou
     * @Date 2019-08-05
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TbFreightParam tbFreightParam) {
        this.tbFreightService.delete(tbFreightParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhou
     * @Date 2019-08-05
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TbFreightParam tbFreightParam) {
        TbFreightResult detail = this.tbFreightService.getWeightPriceById(tbFreightParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhou
     * @Date 2019-08-05
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TbFreightParam tbFreightParam) {
        return this.tbFreightService.getWeightPriceList(tbFreightParam);
    }


}


